package com.base.cn.platform.os.controller.upload;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.service.upload.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
/**
 * 文件上传Controller
 *
 * @author s.li
 * @create 2018-04-04-18:23
 */
@RequestMapping("/admin")
@Controller
public class FileUploadController extends AdminBaseController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传文件
     * @param file
     * @param fileParams
     * @return
     */
    @PostMapping(value = "/uploadFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public Map<String,Object> uploadFile(@RequestPart(value = "file") MultipartFile file,
                                         @RequestParam(value = "fileParams",required = false) String fileParams) throws Exception{
        return fileUploadService.uploadFile(file,fileParams);
    }
    /**
     * 初始化上传文件页面
     * @param model
     * @return 操作页面
     */
    @RequestMapping("/initUploadPage")
    public String initUploadPage(Model model,
                                 @RequestParam(value = "params",required = false) String params){
        String btnId = "btn_"+StringUtils.getRandStr(6)+System.currentTimeMillis();
        model.addAttribute("btnId",btnId);
        model.addAttribute("params",params);
        return resultPage("/upload/upload-file");
    }
}
